package cn.ecut.tbjie.interceptor;

import cn.ecut.tbjie.annotation.NoToken;
import cn.ecut.tbjie.service.TokensService;
import cn.ecut.tbjie.utils.JSONResult;
import cn.ecut.tbjie.utils.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;

@Component
@Slf4j
public class TokenIntercept implements HandlerInterceptor {

    @Autowired
    TokensService tokensService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        //是不是映射到方法上
        boolean isHandlerMethod = handler instanceof HandlerMethod;
        if(!isHandlerMethod){
            JSONObject jsonObject = new JSONObject(JSONResult.errorMsg("非法访问"));
            PrintWriter out = response.getWriter();
            out.write(jsonObject.toString());
            out.close();
            return false;
        }
        //不需要登录的注解
        boolean isNoNeedLogin= ((HandlerMethod) handler).getMethodAnnotation(NoToken.class)!=null;
        if(isNoNeedLogin){
            return true;
        }

        //验证token
        String token = request.getHeader("token");
        String id = request.getParameter("peopleId");
        log.info("token = {}",token);
        if("".equals(token) || !Objects.nonNull(token)){
            JSONObject jsonObject1 = new JSONObject(JSONResult.TokenErr("未找到Token"));
            PrintWriter out = response.getWriter();
            out.write(jsonObject1.toString());
            out.close();
            return false;
        }
        int i = tokensService.verificationToken(token,id);
        if(i == -1){
            JSONObject jsonObject = new JSONObject(JSONResult.TokenErr("Token超时"));
            PrintWriter out = response.getWriter();
            out.write(jsonObject.toString());
            out.close();
            return false;
        }else if(i == 1){
            return true;
        }else if(i == -2) {
            JSONObject jsonObject = new JSONObject(JSONResult.TokenErr("Token与peopleId不匹配"));
            PrintWriter out = response.getWriter();
            out.write(jsonObject.toString());
            out.close();
            return false;
        }else{
            JSONObject jsonObject1 = new JSONObject(JSONResult.TokenErr("Token无法匹配"));
            PrintWriter out = response.getWriter();
            out.write(jsonObject1.toString());
            out.close();
            return false;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
